package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreMenu;
import com.dingreading.cloud.admin.entity.table.StoreMenuTableDef;
import com.dingreading.cloud.admin.mapper.StoreMenuMapper;
import com.dingreading.cloud.admin.service.StoreMenuService;
import com.dingreading.cloud.common.util.NepUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.StoreRoleMenuTableDef.storeRoleMenu;


/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-24
 */
@Service
public class StoreMenuServiceImpl extends BaseServiceImpl<StoreMenuMapper, StoreMenu, StoreMenuTableDef> implements StoreMenuService {

    @Override
    protected StoreMenuTableDef getTable() {
        return StoreMenuTableDef.storeMenu;
    }

    @Override
    public List<StoreMenu> getAllList() {
        QueryWrapper wrapper = QueryWrapper.create()
                .orderBy(table.parentId.asc(), table.orderNum.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean updateNodes(Long id, String nodes) {
        return UpdateChain.of(StoreMenu.class)
                .set(table.nodes, nodes)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<StoreMenu> getByNodesNotId(String nodes, Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.nodes.likeLeft(nodes));
        if (id != null)
            condition.and(table.id.ne(id));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public int dropBean(Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id));

        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<StoreMenu> getByRoleId(Long roleId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(storeRoleMenu.menuId)
                .from(storeRoleMenu)
                .where(storeRoleMenu.roleId.eq(roleId));

        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.in(wrapper));

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectListByQuery(queryWrapper);
    }

    @Override
    public int getMaxShowOrder(Long parentId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(table.parentId.eq(parentId));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public StoreMenu getGtOrderNum(Long parentId, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.parentId.eq(parentId))
                .and(table.orderNum.gt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreMenu getLtOrderNum(Long courseTypeId, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.parentId.eq(courseTypeId))
                .and(table.orderNum.lt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public void updateOrderNum(Long id, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id));

        UpdateChain.of(StoreMenu.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public boolean updateEnabled(Long id, Integer enabled, String headStaffUid, String headStaffName) {
        return UpdateChain.of(StoreMenu.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(headStaffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(headStaffName))
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public boolean updateChildEnabled(String nodes, Integer enabled, String headStaffUid, String headStaffName) {
        return UpdateChain.of(StoreMenu.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(headStaffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(headStaffName))
                .where(table.nodes.likeLeft(nodes))
                .update();
    }

}
